<?php
class Pager
{
	var $url;
	var $pageUrl;
	var $totalRecord;
	var $totalPage;

	var $rewrite;
	var $pageNow;
	var $size;
	var $maxPage;

	function Pager($url, $pageUrl = 'page<NUM>/', $rewrite = false, $maxPage = -1)
	{
		$this->url     = $url;
		$this->pageUrl = $pageUrl;
		$this->rewrite = $rewrite;
		$this->maxPage = $maxPage;
	}

	function build($pageNow, $size, $totalRecord, $maxPager = 50)
	{
		$totalPage = ceil($totalRecord / $size);
		
		if($this->maxPage != -1 && $totalPage > $this->maxPage) {
			$totalPage = $this->maxPage;
		}
		
		$this->totalPage = ($totalPage > $this->totalPage && 0 != $this->totalPage) ? $this->totalPage : $totalPage;
		$this->pageNow = ($pageNow > $this->totalPage) ? $this->totalPage : $pageNow;
		$this->size = $size;
		$this->totalRecord = $totalRecord;
	}

	function html($num, $str = '首页,上一页,当前页,下一页,末页')
	{
		$html = "";
		$str = explode(',', $str);
		$haf = (int)($num / 2);
		$haft = ($num % 2 == 0) ? 1 : 0;
		$begin = $this->pageNow - $haf + $haft;
		$end = $this->pageNow +  $haf;
		$begin = ($this->totalPage - $this->pageNow < $haf) ? $this->totalPage  - $num + 1 : $begin;
		$begin = ($begin < 1) ? 1 : $begin;
		$end = ($this->pageNow < $haf + 1) ? $num : $end;
		$end = ($end > $this->totalPage) ? $this->totalPage : $end;

		$begin = ($begin < 1) ? 1 : $begin;
		$end = ($end > $this->totalPage) ? $this->totalPage : $end;

		if ($this->pageNow > 1)
		{
			$last = $this->pageNow - 1;
			$url = str_replace('<NUM>', 1, $this->pageUrl);
			$html .= '<a href="'. get_rewrite_url(str_replace('<PAGE>', $url, $this->url), $this->rewrite). '" title="首页">'. $str[0]. '</a>';
			$url = str_replace('<NUM>', $last, $this->pageUrl);
			$html .= ' <a href="'.   get_rewrite_url(str_replace('<PAGE>', $url, $this->url), $this->rewrite). '" title="'. $str[1]. '">'. $str[1]. '</a>';
		}

		for ($i = $begin; $i <= $end; $i++)
		{
			if ($this->pageNow == $i)
			{
				$html .= '<span>'. $i. '</span>';
			}
			else
			{
				$url = str_replace('<NUM>', $i, $this->pageUrl);
				$html .= ' <a href="'. get_rewrite_url(str_replace('<PAGE>', $url, $this->url), $this->rewrite). '" title="第 '. $i .' 页">'. $i. '</a>';
			}
		}

		if ($this->pageNow < $this->totalPage)
		{
			$next = $this->pageNow + 1;
			if($this->totalPage > 5 && $this->totalPage > $num && $next + 1 < $this->totalPage)
			{
				$url = str_replace('<NUM>', $this->totalPage, $this->pageUrl);
				$html .= '... <a href="'. get_rewrite_url(str_replace('<PAGE>', $url, $this->url), $this->rewrite). '" title="末页">'. $this->totalPage. '</a>';
			}
			$url = str_replace('<NUM>', $next, $this->pageUrl);
			$html .= ' <a href="'.  get_rewrite_url(str_replace('<PAGE>', $url, $this->url), $this->rewrite). '" title="'. $str[3]. '">'. $str[3]. '</a>';
		}

		return $html;
	}

	function getOffset($pageNow = '', $size = '')
	{
		$pageNow = $pageNow ? $pageNow : $this->pageNow;
		$size = $size ? $size : $this->size;
		$from = ($pageNow - 1) * $size;
		$from = $from < 0 ? 0 : $from;
		return $from;
	}
}
?>